package com.lagou.server;

import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.List;

/**
 * 编程实现服务器的主功能
 */
public class ServerView {
    /**
     * 使用合成复用原则
     */
    private ServerInitClose sic;
    private ServerDao sd;
    /**
     * 通过构造方法实现成员变量的初始化
     * @param sic
     */
    public ServerView(ServerInitClose sic,ServerDao sd){
        this.sic = sic;
        this.sd = sd;
    }

    /**
     * 自定义成员方法实现客户端发来消息的接收并处理
     */
    public void serverReceive() throws IOException, ClassNotFoundException {
        UserMessage tum = (UserMessage) sic.getOis().readObject();
        System.out.println("接收到的消息是："+tum);

        // 调用方法实现管理员账户和密码信息的校验
        if (sd.serverManagerCheck(tum.getUser())){
            tum.setType("success");
        }else {
            tum.setType("fail");
        }
        //将校验结果发送给客户端
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送校验结果成功！");
        /**
         * 实现学员管理模块
         */
        ServerInitClose siclst =new ServerInitClose();


        //调用get方法获取集合
        while (true) {
            UserMessage tub = (UserMessage) sic.getOis().readObject();
            System.out.println("接收到的消息是："+tub);
            switch(tub.getType()){
                case "add":
                    System.out.println("执行添加操作");
                    siclst.getLst().add(tub.getUser());
                    tub.setType("addsuccess");
                    sic.getOos().writeObject(tub);
                    System.out.println("list = "+ siclst.getLst());
                    siclst.setLst(siclst.getLst());
                    break;
                case "re":
                    System.out.println("执行修改操作");
                    siclst.getLst().remove(tub.getUser());
                    sic.getOos().writeObject(tub);
                    System.out.println("list = "+ siclst.getLst());
                    siclst.setLst(siclst.getLst());
                    break;
                case "re1":
                    System.out.println("执行修改操作");
                    siclst.getLst().add(tub.getUser());
                    tub.setType("resuccess");
                    sic.getOos().writeObject(tub);
                    System.out.println("list = "+ siclst.getLst());
                    siclst.setLst(siclst.getLst());
                    break;
                case "del":
                    System.out.println("执行删除操作");
                    siclst.getLst().remove(tub.getUser());
                    System.out.println(tub.getUser());
                    System.out.println("list = "+ siclst.getLst());
                    tub.setType("delsuccess");
                    sic.getOos().writeObject(tub);
                    siclst.setLst(siclst.getLst());
                    break;
                case "at":
                    System.out.println("执行查询操作");
                    int b1 = siclst.getLst().indexOf(tub.getUser());
                    System.out.println("b1 ="+b1);
                    System.out.println("list = "+ siclst.getLst());
                    if (-1 == b1){
                        tub.setType("atfail");
                    }else{
                        tub.setType("atsuccess");
                    }
                    sic.getOos().writeObject(tub);
                    sic.getOos().writeObject(siclst.getLst());
                    siclst.setLst(siclst.getLst());
                    break;
                case "esc":
                    tub.setType("esc");
                    sic.getOos().writeObject(tub);
                    siclst.setLst(siclst.getLst());
                    return;
            }
        }
        //调用set方法设置修改后的集合


    }

}
